/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.spray;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.event.ChangeListener;
import nl.cloudfarming.client.model.ConcentrationDosageUnit;
import nl.cloudfarming.client.model.CropProtection;
import nl.cloudfarming.client.model.DosageUnit;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningService;
import org.openide.WizardDescriptor;
import org.openide.WizardValidationException;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;

public class CropProtectionController implements WizardDescriptor.ValidatingPanel {

    public static final String CROP_PROTECTION = "Crop protections";
    private List<CropProtection> cpList;
    private final String INVALID_SELECTED_CROPPROTECIONS = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.planning_visual_panel_crop_protection_edit_properties.validation.invalid_cropprotection_selection");
    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private CropProtectionPanel component;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public Component getComponent() {
        if (component == null) {
            component = new CropProtectionPanel();
        }
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx(SampleWizardPanel1.class);
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return true;
        // If it depends on some condition (form filled out...), then:
        // return someCondition();
        // and when this condition changes (last form field filled in...) then:
        // fireChangeEvent();
        // and uncomment the complicated stuff below.
    }

    @Override
    public final void addChangeListener(ChangeListener l) {
    }

    @Override
    public final void removeChangeListener(ChangeListener l) {
    }

    // You can use a settings object to keep track of state. Normally the
    // settings object will be the WizardDescriptor, so you can use
    // WizardDescriptor.getProperty & putProperty to store information entered
    // by the user.
    @Override
    public void readSettings(Object settings) {
        TacticalPlanningService tps = new TacticalPlanningService();
        // DUMMY DATA
        //FIXME: This is for test puroses only BEGIN
        cpList = tps.findAllCropProtections();
        if (cpList.isEmpty()) {
            CropProtection.Builder cropProtectionDummy1 = new CropProtection.Builder();
            cropProtectionDummy1.setName("Dummy_Roundup");
            cropProtectionDummy1.setLastUsage(new Date());
            cropProtectionDummy1.setDosageUnit(DosageUnit.KILOGRAM);
            cropProtectionDummy1.setConcentrationDosageUnit(ConcentrationDosageUnit.KG_ACREAGE);

            CropProtection.Builder cropProtectionDummy2 = new CropProtection.Builder();
            cropProtectionDummy2.setName("Dummy_Starane");
            cropProtectionDummy2.setLastUsage(new Date());
            cropProtectionDummy2.setDosageUnit(DosageUnit.LITER);
            cropProtectionDummy2.setConcentrationDosageUnit(ConcentrationDosageUnit.LITER_ACREAGE);

            CropProtection.Builder cropProtectionDummy3 = new CropProtection.Builder();
            cropProtectionDummy3.setName("Dummy_Signum");
            cropProtectionDummy3.setLastUsage(new Date());
            cropProtectionDummy3.setDosageUnit(DosageUnit.GRAM);
            cropProtectionDummy3.setConcentrationDosageUnit(ConcentrationDosageUnit.GRAM_ACREAGE);

            CropProtection.Builder cropProtectionDummy4 = new CropProtection.Builder();
            cropProtectionDummy4.setName("Dummy_Acrobat");
            cropProtectionDummy4.setLastUsage(new Date());
            cropProtectionDummy4.setDosageUnit(DosageUnit.KILOGRAM);
            cropProtectionDummy4.setConcentrationDosageUnit(ConcentrationDosageUnit.KG_ACREAGE);
            tps.createCropProtection(cropProtectionDummy1);
            tps.createCropProtection(cropProtectionDummy2);
            tps.createCropProtection(cropProtectionDummy3);
            tps.createCropProtection(cropProtectionDummy4);
            
        }
        //FIXME: This is for test puroses only END

        cpList = tps.findAllCropProtections();

        component.setSelectedCropProtections(cpList);

    }

    @Override
    public void storeSettings(Object settings) {
        String[][] cropProtections = ((CropProtectionPanel) getComponent()).getSelectedCropProtections();
        ArrayList<CropProtection> selectedCropProtections = new ArrayList<CropProtection>();

        for (CropProtection cropProtection : cpList) {
            for (int i = 0; i < cropProtections.length; i++) {
                if (cropProtections[i][0].equals(cropProtection.getName())) {
                    selectedCropProtections.add(cropProtection);
                }
            }

        }
        ((WizardDescriptor) settings).putProperty(CROP_PROTECTION, selectedCropProtections);
    }

    @Override
    public void validate() throws WizardValidationException {
        String[][] selectedCropProtections = ((CropProtectionPanel) component).getSelectedCropProtections();
        if (selectedCropProtections != null) {
            if (selectedCropProtections.length == 0) {
                throw new WizardValidationException(null, INVALID_SELECTED_CROPPROTECIONS, null);
            }
        }
    }
}
